package booksystem.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.*;

/**
 * @program: ServletDom
 * @description: 验证码生成工具类
 **/
public class VerifyCodeUtils {
    private static final Integer W = 100; //设置宽度
    private static final Integer H = 34;  //设置高度
    //设置验证码生成库
    private static final String CODE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    //设置验证码的字符数量
    private static final Integer CODE_COUNT = 4;
    //干扰线的数量
    private static final Integer LINE_COUNT = 4;

    //创建随机数对象
    static Random random = new Random();

    /**
     * 封装获取Map键值对
     *
     * @return
     */
    public static Map<String, BufferedImage> getMap() {
        //构建一张空白透明的图片，用来生成验证码
        BufferedImage image = new BufferedImage(W, H, BufferedImage.TYPE_INT_BGR);
        //创建画笔；
        Graphics g = image.getGraphics();
        //设置画笔的颜色
        g.setColor(Color.white);//存放随机颜色
        //给图片填充背景色
        g.fillRect(0, 0, W, H);
        //创建字体对象
        Font font = new Font("黑体", Font.BOLD, 25);
        //设置字体
        g.setFont(font);
        //创建可追加字符对象
        StringBuffer sb = new StringBuffer();
        //循环写入验证码
        for (int i = 0; i < CODE_COUNT; i++) {
            //设置画笔颜色，写入字体的颜色
            g.setColor(getColor());
            //获取验证码字符
            char c = getChar();
            //绘制到图片中               位置          高度
            g.drawString(c + "", i * 20 + 5, 25);
            //c+""：将参数c装换成字符串
            sb.append(c);
        }
        //绘制干扰线
        for (int i = 0; i < LINE_COUNT; i++) {
            //设置画笔颜色
            g.setColor(getColor());
            //两点确定一条线
            int xStart = random.nextInt(W + 1);
            int yStart = random.nextInt(H + 1);
            int xEnd = random.nextInt(W + 1);
            int yEnd = random.nextInt(H + 1);
            //绘制线条
            g.drawLine(xStart, yStart, xEnd, yEnd);
        }
        //画噪点
        int total = (int) (W * H * 0.02);
        for (int i = 0; i < total; i++) {
            //设置画笔颜色
            g.setColor(getColor());
            int x = random.nextInt(W);
            int y = random.nextInt(H);
            //绘制点
            image.setRGB(x, y, getColor().getRGB());
        }
        Map<String, BufferedImage> map = new HashMap<>();
        map.put(sb.toString(), image);
        return map;
    }

    //封装得到图片静态方法
    public static BufferedImage getImage(Map<String, BufferedImage> map) {
        Set<Map.Entry<String, BufferedImage>> set = map.entrySet();
        //得到set迭代器
        Iterator<Map.Entry<String, BufferedImage>> iterator = set.iterator();
        //由于此处mao只有一组织，直接得到键值对
        Map.Entry<String, BufferedImage> entry = iterator.next();
        return entry.getValue();
    }

    //封装得到数值静态方法
    public static String getCode(Map<String, BufferedImage> map) {
        Set<Map.Entry<String, BufferedImage>> set = map.entrySet();
        //得到set迭代器
        Iterator<Map.Entry<String, BufferedImage>> iterator = set.iterator();
        //由于此处mao只有一组织，直接得到键值对
        Map.Entry<String, BufferedImage> entry = iterator.next();
        return entry.getKey();
    }

    /**
     * 封装随机获取颜色
     *
     * @return
     */
    private static Color getColor() {
        return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
    }

    /**
     * 封装获取验证码字符方法
     *
     * @return
     */
    private static char getChar() {
        //在仓库CODE中随机获取一个
        return CODE.charAt(random.nextInt(62));
    }


}
